﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Model.Entities;
using CSP.Model.Model;
using Twitterizer.Framework;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Diagnostics;
namespace CSP.Model.Presenter
{
    public class WhatsOnYourMindPresenter
    {
        private IWhatsOnYourMindPresenter view = null;
        private ModelManager model = new ModelManager();

        public WhatsOnYourMindPresenter(IWhatsOnYourMindPresenter iView)
        {
            view = iView;
        }

        public void Load()
        {
            try
            {
                view.Title = "";
                view.UserImage = model.GetCurrentUserImage();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void PostPeep()
        {
            try
            {
                ModelManager m = new ModelManager();
                string userImage = m.GetCurrentUserImage();
                Peep peep = new Peep(view.Title, view.User, view.Date, DateTime.Now.TimeOfDay.ToString(), userImage, view.PostToFB, view.PostToTwitter);
                SPWebApplication webApp = SPContext.Current.Site.WebApplication;
                SPSiteCollection siteColl = webApp.Sites;


                //siteColl = SPContext.Current.Current.Site;

                string webAppUrl = siteColl[0].Url;

                //site = SPContext.Current.Web;
                //string rootweb = site.ParentWeb.Url;
                //view.SiteURL = rootweb;
                //Write to the site collection

                //write to the root location
                //result = model.PostPeep(peep, view.ListName, rootweb);
                string t = peep.Title;

                if (view.PostToTwitter)
                {
                    if (t.Length > 140)
                        t = t.Substring(0, 140);

                    if (view.PostToFB)
                    {
                        if (t.Length > 136)
                            t = t.Substring(0, 136);
                        t += " #fb";
                    }

                    try
                    {
                        //Twitter tweet = new Twitter(view.User, view.Password, "Gov 2.0 My Peeps");
                        Twitter tweet = new Twitter(view.User, view.Password);
                        tweet.Status.Update(t);
                    }
                    catch 
                    {
                        //Do nothing, the post is successfull but for some reason the DLL rthroughs an uinecessary error.
                    }
                }

                bool result = model.PostPeep(peep, view.ListName, webAppUrl);
            }
            catch (Exception ex)
            {

            }
        }

        public void DeleteAllPeeps()
        {
            model.DeleteAllPeeps(view.ListName, view.SiteURL);
        }

    }
}
